import math

def mean(x):
	"""
	Returns the arithmetic mean of a list of numbers.
	"""
	return sum(x) / len(x)
	
def std(x):
	"""
	Returns the standard deviation of a list of numbers.
	"""
	m = mean(x)
	total = 0
	for i in x:
		total = total + (i - m)**2
	total = float(total) / len(x)
	return math.sqrt(total)
	
def distance(lat1,lon1,lat2,lon2):
	"""
	Returns the as-the-crow-flies distance between any two geographic points.
	"""
	pi = 3.1415926535;
	lat1 = lat1 * pi / 180.0;
	lat2 = lat2 * pi / 180.0;
	lon1 = lon1 * pi / 180.0;
	lon2 = lon2 * pi / 180.0;
	
	# radius of earth, in meters.
	R = 6371000;

	dlat = lat2- lat1;
	dlon = lon2- lon1;
	
	a = math.sin(dlat/2)* math.sin(dlat/2) + math.cos(lat1)* math.cos(lat2)* math.sin(dlon/2)* math.sin(dlon/2);
	
	c = 2* math.atan2(math.sqrt(a), math.sqrt(1-a));
	
	return R*c;
	
def cross(x1,y1,z1,x2,y2,z2):
	"""
	The cross product.
	"""
	i = y1 * z2 - z1 * y2
	j = z1 * x2 - x1 * z2
	k = x1 * y2 - y1 * x2
	return [i,j,k]

def normal(x0,y0,z0,x1,y1,z1,x2,y2,z2):
	"""
	Finds some normal given by three (x,y,z) vertices of a triangle.
	Returns the list [nx,ny,nz], which is normalized.
	"""
	epsilon = 0.001;
	aX = x1 - x0
	aY = y1 - y0
	aZ = z1 - z0
	bX = x2 - x0
	bY = y2 - y0
	bZ = z2 - z0
	[nX, nY, nZ] = cross(aX,aY,aZ,bX,bY,bZ)
	magnitude = math.sqrt(nX * nX + nY * nY + nZ * nZ)
	nX = nX / magnitude
	nY = nY / magnitude
	nZ = nZ / magnitude
		
	return [nX, nY, nZ]
	
def mean(L):
    return sum(L) / len(L)
    
def std(L):
    mu = mean(L);
    deviations = [(mu - x)*(mu - x) for x in L]
    variance = sum(deviations)
    return math.sqrt(variance / (len(L) - 1))
    